package com.lawyer.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.lawyer.entity.User;

@RepositoryRestResource(path = "users", collectionResourceRel = "users")
public interface UserRepository extends PagingAndSortingRepository<User, String> , JpaSpecificationExecutor<User> {
	
	User findByUsername(String username);

	List<User> findByAgentId(String agentId);

	Page<User> findByAgentId(String agentId, Pageable pageable);

	User findByQrCode(String qrCode);
	

	@Query(value = "select MAX(qr_code)+ 1 from user", nativeQuery = true)
	Integer queryMaxCode();
	
	/**
	 * 当天新注册用户
	 */
	@Query(value = "select count(id) from User where to_days(createdDate) = to_days(now())")
	Object registerCurrDay();
	/**
	 * 最近7天用户注册量
	 */
	@Query(value = "select count(id) from user where date_sub(curdate(), interval 7 day) <= date(created_date)", nativeQuery = true)
	Object register7Day();
	/**
	 * 最近30天用户注册量
	 */
	@Query(value = "select count(id) from user where date_sub(curdate(), interval 30 day) <= date(created_date)", nativeQuery = true)
	Object register30Day();
	
	/**
	 * 总用户量
	 */
	@Query(value = "select count(id) from user", nativeQuery = true)
	Object totalUserCount();

	
	public List<User> findAll();
}
